home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Burning & Media / GB-PVR 1.2.13 / GBPVR10213.msi / Cabs.w1.cab / PhotoDirectory.aspx.cs428 < prev    next >
Text File  |  2008-01-01  |  10KB  |  221 lines

  1. using System;
  2. using System.IO;
  3. using System.Text.RegularExpressions;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6. using System.Xml;
  7.  
  8. namespace gbweb
  9. {
  10.     /// <summary>
  11.     /// Summary description for Search.
  12.     /// </summary>
  13.         public partial class PhotoDirectory : Page
  14.         {
  15.  
  16.             private Regex filter = new Regex(Global.Settings.photoFiles, RegexOptions.Compiled | RegexOptions.IgnoreCase);
  17.  
  18.             protected void Page_Load(object sender, EventArgs e)
  19.             {
  20.                 //If first time page is being displayed
  21.                 if (!IsPostBack)
  22.                 {
  23.                     //Default the directorylist variable
  24.                     string directorylist = "Photo~|";
  25.                     //Get the photo libraries from the GBPVR Config File
  26.                     XmlNode node = Global.Config.SelectSingleNode("/settings/PhotoLibraryDirectory");
  27.                     //Create arrays to hold the libraries and directories within the libraries
  28.                     string[] directories = new string[500];
  29.                     string[] libraries = new string[500];
  30.                     //If a Photo Library was found in the config
  31.                     if (node != null)
  32.                     {
  33.                         //Get the list of libraries and directories from the node
  34.                         directorylist = node.InnerText;
  35.                         //Split eaach record from the node into a Libarary and Directory
  36.                         string[] directorymappings = directorylist.Split('|');
  37.                         int i = 0;
  38.                         //Sort the array of libraries and directories
  39.                         Array.Sort(directorymappings);
  40.                         //Loop through the array laoding the Libraries and Directories to their own array
  41.                         foreach (string directorymapping in directorymappings)
  42.                         {
  43.                             //If there is something in the library directory record load each piece to the array
  44.                             if (directorymapping != "")
  45.                             {
  46.                                 //Split the library directory record into the two seperate pieces
  47.                                 string[] mapping = directorymapping.Split('~');
  48.                                 //If there is a library found load the appropriate array
  49.                                 if ((directorymapping.Length > 0) && (mapping.Length > 1))
  50.                                 {
  51.                                     libraries[i] = mapping[0];
  52.                                     directories[i] = mapping[1];
  53.                                 }
  54.                                 //Increment the index used for loading the libaray and directory array
  55.                                 i++;
  56.                             }
  57.                         }
  58.  
  59.                         //Resize the arrays to free up unused space
  60.                         Array.Resize(ref libraries, i);
  61.                         Array.Resize(ref directories, i);
  62.                         //Loop through each entry in the directory array to build the list of libraries and directories
  63.                         //that is loaded into the tree
  64.  
  65.                         for (i = 0; i < directories.Length;i++ )
  66.                         {
  67.                             //Create a new set of nodes for holding all the info
  68.                             TreeNodeCollection oNodes = new TreeNodeCollection();
  69.                             //Call the method to build and load the directories for loading to the treeview control
  70.                             BuildTree(directories[i], oNodes);
  71.                             // Create a node for the library name
  72.                             TreeNode oNode = new TreeNode();
  73.                             //Set the node name to the name of the library
  74.                             oNode.Text = libraries[i];
  75.                             //Set the action to occur when the library node is clicked on
  76.                             oNode.SelectAction = TreeNodeSelectAction.Expand;
  77.                             //Loop through the nodes that contain all the directy and file information and load them into
  78.                             //the library node....this makes them show up under the libaray
  79.                             foreach (TreeNode tnode in oNodes)
  80.                             {
  81.                                 oNode.ChildNodes.Add(tnode);
  82.                             }
  83.                             //Set the library node to be collapsed
  84.                             oNode.Expanded = false;
  85.                             //Add the library node (which now contains all the directoy and file nodes) to the treeview control
  86.                             TreeView1.Nodes.Add(oNode);
  87.                         }
  88.                         //Set the images used by the treeview control to be like the windows file explorer icons
  89.                         TreeView1.ImageSet = TreeViewImageSet.XPFileExplorer;
  90.                     }
  91.                     //Shrink the arrays down to 0
  92.                     Array.Resize(ref directories, 0);
  93.                     Array.Resize(ref libraries, 0);
  94.                 }
  95.  
  96.             }
  97.  
  98.             #region Web Form Designer generated code
  99.             override protected void OnInit(EventArgs e)
  100.             {
  101.                 //
  102.                 // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  103.                 //
  104.                 InitializeComponent();
  105.                 base.OnInit(e);
  106.             }
  107.  
  108.             /// <summary>
  109.             /// Required method for Designer support - do not modify
  110.             /// the contents of this method with the code editor.
  111.             /// </summary>
  112.             private void InitializeComponent()
  113.             {
  114.  
  115.             }
  116.             #endregion
  117.  
  118.         
  119.         
  120.     // Recursively loops through folder structure and 
  121.     // loads all folders and files into TreeView. 
  122.     // Each iteration gets files and folders 
  123.     // immediately within current folder.
  124.         
  125.             private void BuildTree(string sPath, TreeNodeCollection oNodes)
  126.             {
  127.                 try
  128.                 {
  129.                     // Load all folders in current path into array
  130.                     string[] aFolders = Directory.GetDirectories(sPath);
  131.                     Array.Sort(aFolders);
  132.                     // Loop through all folders in current path, 
  133.                     // create nodes for them and load them into 
  134.                     // TreeView
  135.                     foreach (string sTempFolder in aFolders)
  136.                     {
  137.                         // Strip path out and retrieve folder name 
  138.                         // only
  139.                         string sFolder = sTempFolder.Substring(sPath.Length + 1);
  140.  
  141.                         //Create node for folder and add it to 
  142.                         oNodes.Add(CreateFolderNode(sFolder));
  143.                     }
  144.  
  145.                     // Load all JPGs in current path into array
  146.                     string[] aFiles = Directory.GetFiles(sPath, "*.jpg");
  147.                     Array.Sort(aFiles);
  148.                     // Loop through files in current path, create 
  149.                     // nodes for them and load them into TreeView
  150.                     foreach (string sFile in aFiles)
  151.                     {
  152.                         // Create node for file; add it to 
  153.                         // TreeView. Pass in path with file name of
  154.                         // file (text for file node)
  155.                         if (filter.IsMatch(sFile))
  156.                         {
  157.                             oNodes.Add(CreateFileNode(sFile, sFile.Substring(sPath.Length + 1)));
  158.                         }
  159.                     }
  160.  
  161.                     // Recursively call GetFolders method for 
  162.                     // subfolder
  163.                     for (int i = 0; i < oNodes.Count; i++)
  164.                     {
  165.                         // If the node is a folder ...
  166.                         if (!filter.IsMatch(oNodes[i].Text.ToLower()))
  167.                         {
  168.                             // Recursively call GetFolders method for 
  169.                             // subfolder
  170.                             //BuildTree(aFolders[i] + "\\", oNodes[i].ChildNodes);
  171.                             BuildTree(aFolders[i], oNodes[i].ChildNodes);
  172.                         }
  173.                     }
  174.                 }
  175.                 catch (Exception e)
  176.                 {
  177.                     return;
  178.                 }
  179.             }
  180.  
  181.     
  182.         // Create file TreeNode from specified path
  183.         private static TreeNode CreateFileNode(string sPath, string sText)
  184.         {
  185.            // Create a node
  186.            TreeNode oNode = new TreeNode();
  187.             
  188.            // Set the display text for the node
  189.            oNode.Text = sText;
  190.            //Set the url of the node so that when clicked it activates the photo.aspx page and passes in the location of the photo.
  191.            //The path of the photo is serialized so that it can be passed around and is not readable by a human.
  192.            //The whole query string is set to utilze the download.ashx page to process the image.
  193.            oNode.NavigateUrl = "Photo.aspx?pic=" + Download.GetDownloadUrl(false, true, Download.InternalFiles.Photo, PublicDownload.Serialize(sPath));
  194.             
  195.            // Point target for hyperlink to iframe
  196.            oNode.Target = "ifImages";
  197.             
  198.            // Return the file node
  199.            return oNode;
  200.         }
  201.  
  202.         // Create a folder TreeNode from the specified path
  203.         private static TreeNode CreateFolderNode(string sText)
  204.         {
  205.             // Create a node
  206.             TreeNode oNode = new TreeNode();
  207.             
  208.             // Set the type to be folder 
  209.             //oNode.Type = "folder";
  210.             
  211.             // Set the display text for the node
  212.             oNode.Text = sText;
  213.             oNode.Expanded = false;
  214.             oNode.SelectAction = TreeNodeSelectAction.Expand;
  215.             
  216.             // Return the folder node
  217.             return oNode;
  218.         }
  219.     }
  220. }
  221.